package com.situ.company.web220111.employee.servlet;

import com.situ.company.web220111.employee.model.EmployeeModel;
import com.situ.company.web220111.employee.service.IEmployeeService;
import com.situ.company.web220111.employee.service.Impl.EmployeeServiceImpl;
import com.situ.company.web220111.util.FmtRequest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "EmployeeServlet", value = "/EmployeeServlet")
public class EmployeeServlet extends HttpServlet {
    private IEmployeeService service = new EmployeeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object res = null;
        switch (request.getParameter("action")) {
            case "reg":
                res = reg(request);
                break;
            case "login":
                res = login(request);
                break;
            case "logout":
                request.getSession().removeAttribute("user");
                response.sendRedirect(request.getContextPath() + "/web/login.jsp"); // 退出用请求重定向比较多
                return;
            case "list":
                res = list(request);
                break;
            case "page":
                res = page(request);
                break;
            case "add":
                res = add(request);
                break;
            case "del":
                res = del(request);
                break;
            case "get":
                res = get(request);
                break;
            case "upd":
                res = upd(request);
                break;
            case "resetPass":
                res = resetPass(request);
                break;
            case "updPass":
                res = updPass(request);
                break;
            case "setDept":
                res = setDept(request);
                break;
            case "delPic":
                res = delPic(request);
        }
        FmtRequest.write(response.getWriter(), res);
    }

    private String delPic(HttpServletRequest request) {
        return service.delPic(FmtRequest.parseModel(request,EmployeeModel.class));
    }

    private String setDept(HttpServletRequest request) {
        return service.setDept(FmtRequest.parseModel(request,EmployeeModel.class));
    }

    private String updPass(HttpServletRequest request) {
        return service.updPass(FmtRequest.parseModel(request, EmployeeModel.class));
    }

    private String resetPass(HttpServletRequest request) {
        return service.resetPass(FmtRequest.parseModel(request, EmployeeModel.class));
    }

    private String login(HttpServletRequest request) {
        EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class);
        String res = service.login(model);
        System.out.println(res);
        if ("1".equals(res)) {
            System.out.println(model);
            request.getSession().setAttribute("user", service.selectModel(model));
            System.out.println(request.getSession().getAttribute("user"));
        }
        return res;
    }

    public String reg(HttpServletRequest req) { // 获取reg数据
        return service.insert(FmtRequest.parseModel(req, EmployeeModel.class));
    }

    private Object page(HttpServletRequest request) {
        EmployeeModel model = new EmployeeModel();
        model.setCode(request.getParameter("code"));
        model.setName(request.getParameter("name"));
        model.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
        model.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
        model.setPageOn(true);
        List<EmployeeModel> list = service.selectList(model); // 带有查询条件和分页条件，得到的当前页面要显示的数据
        Integer count = service.selectCount(model);  // 带有查询条件，不带有分页条件，得到的记录条数
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return map;
    }

    private String upd(HttpServletRequest request) {
        return service.updateAll(FmtRequest.parseModel(request, EmployeeModel.class));
    }

    private Object get(HttpServletRequest request) {
        return service.selectModel(FmtRequest.parseModel(request, EmployeeModel.class));
    }

    private String del(HttpServletRequest request) {
        return service.delete(new EmployeeModel(request.getParameter("code")));
        //return service.delete(FmtRequest.parseModel(request,DepartmentModel.class));
    }

    private String add(HttpServletRequest request) {
        // 把得到的参数依次映射到实体类中
        return service.insert(FmtRequest.parseModel(request, EmployeeModel.class));
    }

    private List<EmployeeModel> list(HttpServletRequest request) {
        return service.selectList(FmtRequest.parseModel(request, EmployeeModel.class));
    }
}
